﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections;
using Telerik.Windows.Controls;
using TestMatrix.Model;
using TestMatrix.BLController;

namespace TestMatrix.Phone
{
    public partial class ReleaseDetailInfoPage : PhoneApplicationPage
    {
        public ReleaseDetailInfoPage()
        {
            InitializeComponent();
            this.Loaded += OnLoad;
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            if (this.ListBox != null)
            {
                this.ListBox.IsAsyncBalanceEnabled = this.EnableAsyncBalance;
            }

            this.isDisplayed = true;
            this.OnDisplayed();
        }

        protected virtual IEnumerable CreateItemsSource()
        {
            string sql = "select ";
            sql += "EBIProjectName," + Environment.NewLine;
            sql += "sum(ReleaseCnt) as ReleaseCnt," + Environment.NewLine;
            sql += "sum(ReleaseTimes) as ReleaseTimes," + Environment.NewLine;
            sql += "sum(TestCaseCnt) as TestCaseCnt," + Environment.NewLine;
            sql += "sum(ChangeSetCnt) as ChangeSetCnt," + Environment.NewLine;
            sql += "sum(FixedBug) as FixedBug," + Environment.NewLine;
            sql += "sum(ChangeRequest) as ChangeRequest," + Environment.NewLine;
            sql += "sum(Feature) as Feature," + Environment.NewLine;
            sql += "sum(Issue) as Issue," + Environment.NewLine;
            sql += "sum(Task) as Task," + Environment.NewLine;
            sql += "sum(UserStory) as UserStory" + Environment.NewLine;
            sql += "from tblReleaseDetailInfo" + Environment.NewLine;
            sql += "group by EBIProjectName;";

            List<tblReleaseDetailInfo> items = ChartController.QuerytblReleaseDetailInfo(sql);

            return items;
        }


        #region basic method

        private IEnumerable itemsSource;
        private bool isDisplayed;

        protected virtual RadDataBoundListBox ListBox
        {
            get
            {
                return this.DataBoundListBox;
            }
        }

        public bool IsDisplayed
        {
            get
            {
                return this.isDisplayed;
            }
        }

        protected virtual void OnDisplayed()
        {
            if (this.ListBox == null)
            {
                return;
            }

            if (this.itemsSource == null)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(this.LoadItemsSource);
            }
            else
            {
                this.DisplayData(this.itemsSource);
            }
        }

        private void LoadItemsSource(object state)
        {
            this.itemsSource = this.CreateItemsSource();
            if (this.isDisplayed)
            {
                this.DisplayData(this.itemsSource);
            }

        }

        protected virtual bool EnableAsyncBalance
        {
            get
            {
                return true;
            }
        }

        protected virtual void DisplayData(IEnumerable items)
        {
            if (this.ListBox == null || !this.isDisplayed)
            {
                return;
            }

            this.Dispatcher.BeginInvoke(() =>
            {
                this.ListBox.BeginAsyncBalance();
                this.ListBox.ItemsSource = items;
                this.OnItemsBound();

            });
        }

        protected virtual void OnItemsBound()
        {
        }

        #endregion
    }
}